﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Common;
using DadayunApi.Model;
using Newtonsoft.Json;
using RestSharp;

namespace DadayunApi
{
    public class UserControlApi : Api
    {
        public UserControlApi(AppAuthBase authBase, string baseUri) : base(authBase, baseUri)
        {
        }
        public Login_Info GetLoginInfo()
        {
            Login_Info obj = null;
            var uri = "/v1/users/login_info";
            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, httpgetTokenHeader);
            if (ans == HttpStatusCode.OK)
            {
                obj = JsonConvert.DeserializeObject<Login_Info>(json);
            }
           
            return obj;
        }
        public List<RoleInfo> GetRoleInfo()
        {
            List<RoleInfo> list = null;
            var uri = "/v1/roles";

            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, httpgetTokenHeader);
            if (ans == HttpStatusCode.OK)
            {
                list = JsonConvert.DeserializeObject<List<RoleInfo>>(json);
            }
            return list;
        }
        public RoleInfo GetRoleInfo(string id)
        {
            RoleInfo list = null;
            var uri = "/v1/roles/" + id;

            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, httpgetTokenHeader);
            if (ans == HttpStatusCode.OK)
            {
                list = JsonConvert.DeserializeObject<RoleInfo>(json);
            }
            return list;
        }
        /// <summary>
        /// 获取角色下岗位列表
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="withId"></param>
        /// <returns></returns>
        public List<IdName> GetPositionsOfRole(string roleId, bool withId = false)
        {
            List<IdName> list = null;
            var uri = "/v1/roles/" + roleId + "/positions?withId=" + withId;
            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, httpgetTokenHeader);
            if (ans == HttpStatusCode.OK)
            {
                list = JsonConvert.DeserializeObject<List<IdName>>(json);
            }
            return list;
        }
        /// <summary>
        /// 获取角色下用户列表
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="withId"></param>
        /// <returns>以ID-Name格式返回角色下用户列表</returns>
        public List<IdName> GetUsersOfRole(string roleId, bool withId = false)
        {
            List<IdName> list = null;
            var uri = "/v1/roles/" + roleId + "/users?withId=" + withId;
            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, httpgetTokenHeader);
            if (ans == HttpStatusCode.OK)
            {
                list = JsonConvert.DeserializeObject<List<IdName>>(json);
            }
            return list;
        }

    }
}
